<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">网关维护信息</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="24">
            <el-form-item label="网关序列号" prop="gatewayNo">
              <el-input v-model="ruleForm.gatewayNo" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="网关名称" prop="gatewayName" label-width="80px">
              <el-input v-model="ruleForm.gatewayName" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="关联设备" prop="model">
              <el-input v-model="ruleForm.model" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="IP地址" prop="mill">
              <el-input v-model="ruleForm.mill" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="安装时间" prop="setupDate" label-width="80px">
              <el-date-picker v-model="ruleForm.setupDate" :clearable="false" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark" maxlength="200" type="textarea" rows="3" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <!-- <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span>-->
  </el-dialog>
</template>
<script>
export default {
  data() {
    const checkGatewayName = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/network/gateway/verifyNoUnique?gatewayName=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('网关名称重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        gatewayNo: '',
        gatewayName: '',
        mill: '',
        model: '',
        setupDate: '',
        remark: ''
      },
      rules: {
        gatewayName: [
          { required: true, message: '网关名称不能为空！', trigger: 'blur' },
          { validator: checkGatewayName, trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.dialogVisible = true
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          console.log(this.ruleForm)
          const res = await this.$axios.post(
            '/network/gateway/save',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        gatewayNo: '',
        gatewayName: '',
        mill: '',
        model: '',
        setupDate: '',
        remark: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
